package de.sordul.jobMaster.util;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import de.sordul.jobMaster.IWordlistDAO;
import de.sordul.jobMaster.impl.JobMasterServiceFile;
import de.sordul.jobMaster.impl.WordlistDAO;

public class BootstrapServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IWordlistDAO wordlistDAO = new WordlistDAO();
	private Logger log = Logger.getLogger(BootstrapServlet.class.toString());
	
	/**
	 * Initialization code.
	 */
	public void init(ServletConfig config) throws ServletException {		
		URL wordlistDir = JobMasterServiceFile.class.getResource("/wordlists");
		File folder;
		
		try {
			folder = new File(wordlistDir.toURI());
			File[] filesList= folder.listFiles();

			// Iterate through all files in the wordlist directory
			for( File f: filesList ) {
				log.info("Adding file '"+f.getName()+"' to database.");
				wordlistDAO.createByFile(f);
			}
			
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}
}